package com.example.demo.controller;

import com.example.demo.config.EncryptUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc:
 * @author: fuxiang
 * @date: 2018/7/26 16:47
 */

@RestController
@RequestMapping("/dic")
public class IKDicController {
    /**
     * Logger
     */
    private static final Logger LOGGER = LogManager.getLogger(IKDicController.class);

    /**
     * IK热部署加载
     *
     * @return
     */
    @RequestMapping("/getMyIKDic")
    public String getMyIKDic(HttpServletResponse response) {
        StringBuilder builder = new StringBuilder();
        try {

            File file = ResourceUtils.getFile("classpath:ikdic.txt");
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
            BufferedReader br = new BufferedReader(reader);
            String line = null;
            while ((line = br.readLine()) != null) {
                builder.append(line).append("\n");
            }
            //response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setHeader("Last-Modified", EncryptUtil.md5(builder.toString()));
            response.setHeader("ETag", EncryptUtil.md5(builder.toString()));
        } catch (Exception e) {
            LOGGER.error("ik is error...", e);
        }
        return builder.toString();
    }
    /**
     * IK热部署加载
     *
     * @return
     */
    @RequestMapping("/remoteDic")
    public String remoteDic(HttpServletResponse response) {
        List<String> list = new ArrayList<>();
        try {

        } catch (Exception e) {
            LOGGER.error(e);
        }
        return builderDic(list, response);
    }

    private String builderDic(List<String> dics, HttpServletResponse response) {
        StringBuilder builder = new StringBuilder();
        builder.append("\n");
        if (dics != null) {
            dics.forEach(s -> builder.append(s).append("\n"));
        }
        //String md5 = EncryptUtil.md5(builder.toString());
      //  response.setHeader("Last-Modified", md5);
       // response.setHeader("ETag", md5);
        return builder.toString();
    }



}

